package de.codecasa.andro.kasse;

import java.util.ArrayList;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.LinearLayout.LayoutParams;

public class ProductKeypad 
{

	public enum KatAlign {Horizontal, Vertikal}
	
	private EventListener mListener;
	
	private TableLayout mainTable;
	private Context context;
	
	private dbActions datasource;
	private List<dsrArtikel> artikelList = new ArrayList<dsrArtikel>();
	
	private Integer aktAID;
	
	
	private KatAlign katAlign;
	
	
	public ProductKeypad(TableLayout table, Context context, dbActions ds)
	{
		this.mainTable = table;
		this.context = context;
		this.datasource = ds;
		
		this.katAlign = KatAlign.Horizontal;
		
		
		this.aktAID = 0;
	
		initButtons();
	}

	
	public void updateButtons()
	{
		initButtons();
	}
	
 
	private void iButtons()
    {
    	mainTable.removeAllViews();
    	
        datasource.open();
        artikelList = datasource.getArtikel("");
        datasource.close();

    	TableRow tr = new TableRow(context);
        
        
		// Alle Artikel durchgehen
        for (dsrArtikel artikel : artikelList) 
 		{

			aktAID = artikel.getId();
        	// add button
            Button b = new Button(context);
            b.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            b.setId(aktAID);
            b.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                  if(mListener != null) mListener.onAddProduct(v.getId());
                }
              });
            
            b.setText(artikel.gettext());
            
            
            
            
            tr.addView(b);
			
		}
        
        mainTable.addView(tr);
 		        
        
    }
	
	private void initButtons()
    {
    	mainTable.removeAllViews();

   
     	
     	Integer katC = 2;
     	for(Integer x = 1 ; x < katC+1 ; x++)
     	{
            
            TableRow tr = new TableRow(context);
            
            if (x == 1) 
            { 
            	tr.setBackgroundColor(Color.rgb(240, 200, 150));
            }
            else tr.setBackgroundColor(Color.CYAN);
            tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));


            datasource.open();
            artikelList = datasource.getArtikel("kat=" + x);
            datasource.close();
            
     		
     		for (dsrArtikel artikel : artikelList) 
     		{

    			aktAID = artikel.getId();
            	// add button
                Button b = new Button(context);
                b.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                b.setId(aktAID);
                b.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                      if(mListener != null) mListener.onAddProduct(v.getId());
                    }
                  });
                
                b.setText(artikel.gettext());
                tr.addView(b);
    			
    		}
     		
     		mainTable.addView(tr);
     	}
     		
    }


        public void setOnAddProductEventListener(EventListener listener) 
        {
            mListener = listener;
        }    	
     	   
  
	
}
